#!/bin/bash
# 此脚本用来编译安装gridss-2.13.2软件
# gridss-2.13.2软件介绍
#  GRIDSS是一个模块软件套件，其中包含可用于检测基因组重排的工具。
#  Usage:
#     bash /path/to/build.sh /path/to/build /path/to/install
# 使用build.sh 脚本样例 bash /path/to/build.sh /path/to/build /path/to/install
# 使用build.sh 需要root权限或者sudo权限,否则无法安装依赖
#
##############################################################

if [[ $# -ne 2 ]]; then
    echo "The format is as follows:"
    echo "bash build.sh buildpath installpath"
    echo ":param buildpath: 应用构建绝对路径"
    echo ":param installpath: 应用安装绝对路径"
    exit 1
fi

[[ ! "$1" =~ ^/.* || "$1" = "/" ]] &&  echo "请输入正确的构建路径" && exit 1
[[ ! "$2" =~ ^/.* || "$2" = "/" ]] &&  echo "请输入正确的安装路径" && exit 1

create_dir()
{
    local path=$1
    [[ "$path" =~ .*/$ ]] && path=${path%/*}
    if [[ ! -d "$path" && ! -f "$path" ]]; then mkdir -p $path; else path=$path`date "+%y%m%d%H%M%S"` && mkdir -p $path; fi
    echo $path
}

buildpath=$(create_dir $1)
installpath=$(create_dir $2)

yum install -y m4 bc zlib-devel zlib environment-modules systemd-devel
source /etc/profile
module purge

set -e
dep_dir="dependency"
  
  cp -r $dep_dir/src/sse2neon.h $buildpath

  bash $dep_dir/bisheng-compiler-2.1.0.sh $buildpath $installpath
  module use $installpath/bisheng-compiler-2.1.0
  module load $installpath/bisheng-compiler-2.1.0/bisheng_modulefiles

  bash $dep_dir/bisheng-jdk-8u322.sh $buildpath $installpath
  module use $installpath/bisheng-jdk-8u322
  module load $installpath/bisheng-jdk-8u322/jdk_modulefiles

  bash $dep_dir/bwa-0.7.17.sh $buildpath $installpath
  module use $installpath/bwa-0.7.17
  module load $installpath/bwa-0.7.17/bwa_modulefiles

  bash $dep_dir/R-4.2.1.sh $buildpath $installpath
  module use $installpath/R-4.2.1
  module load $installpath/R-4.2.1/R-4.2.1_modulefiles

  bash $dep_dir/samtools-1.15.1.sh $buildpath $installpath
  module use $installpath/samtools-1.15.1
  module load $installpath/samtools-1.15.1/samtools_modulefiles

  bash gridss-2.13.2.sh $buildpath $installpath
  mkdir -p pkg/gridss-2.13.2-hpc && cp -r $installpath/* pkg/gridss-2.13.2-hpc/

  rm -rf pkg/gridss-2.13.2-hpc/R-4.2.1
  cp $dep_dir/R-4.2.1.sh pkg/gridss-2.13.2-hpc/

  cat >"pkg/gridss-2.13.2-hpc/run.sh" <<EOF
#!/bin/bash
yum install -y wget tar libatomic environment-modules Cython cmake python\\ 
  git python python2-numpy* readline-devel libXt-devel bzip2-devel xz-devel \\
  libcurl-devel glibc-headers gcc-c++ pcre* ncurses-devel ncurses curl-devel \\
  openssl openssl-devel gcc-gfortran time texinfo texlive

source /etc/profile &>/dev/null

current_dir=\$(cd "\$(dirname "\${BASH_SOURCE[0]}")" &> /dev/null && pwd)
for d in \$(ls \$current_dir)
do
     [[ ! \$d =~ "scripts" && ! -f "\$current_dir/\$d" ]] && cd \$current_dir/\$d && module use \$(pwd) && module load \$(pwd)/*modulefiles
done

# 编译 R 并初始化其环境变量
mkdir "\${current_dir}"/build
bash "\${current_dir}"/R-4.2.1.sh "\${current_dir}"/build "\${current_dir}"
module use "\${current_dir}"/R-4.2.1
module load "\${current_dir}"/R-4.2.1/R-4.2.1_modulefiles
rm -rf "\${current_dir}"/build

echo -e "\033[1;32;1mgridss environment initialization completed.\033[0m"

EOF

  chmod +x pkg/gridss-2.13.2-hpc/run.sh
   rm -rf $buildpath $installpath

  echo "# 正在打包 gridss 文件"
  cd pkg && tar -zcf gridss-2.13.2-hpc.tar.gz gridss-2.13.2-hpc
  echo -e "\033[1;32;1m# gridss 已经打包成功\033[0m"

  echo " # ------------------------------------------------------------------------"
  echo "# 运行软件前请执行以下命令"
  echo -e "\033[1;32;1m# source pkg/gridss-2.13.2-hpc/run.sh\033[0m"
  echo "# 来加载 gridss 运行时环境变量"
  echo "# ------------------------------------------------------------------------"

